<?php

/*************************************************************************

	FILE INCLUDE

**************************************************************************/
include_once '../lib/Template.class.php';
include_once '../lib/class.file.php';
$debug_mode_value = 0;
setlocale(LC_CTYPE, 'ko_KR.eucKR');


/*************************************************************************

	USER SCRIPT

***************************************************************************/
$userscript =<<<js
    <script type="text/javascript">
        <!--

        //-->
    </script>
js;


/*************************************************************************

		PAGE MAIN PROCESS

***************************************************************************/
(!empty($_POST['domain']))? $domain = $_POST['domain'] : $domain = "";
(!empty($_POST['ner']))? $ner = $_POST['ner'] : $ner = 1;
(!empty($_SESSION[user_cd]))? $user_cd = $_SESSION[user_cd] : $user_cd = 1;

print_rr($_SESSION);
print_rr($_FILES);

$file_nm=$_FILES[file][name];
$old_file=$_FILES[file][tmp_name];


if(empty($file_nm)){
	$JSClass->msg_replace("Please attach file","./SynonymGenerator.php");
}

if($FileClass->check_file_type($file_nm,"csv,CSV")){
	$JSClass->msg_replace("csv only","./SynonymGenerator.php");
}

$dir="UPLOAD_FILES/".rand(1,10);
$FileClass->make_dir($dir);

$new_nm=$FileClass->make_name($file_nm);


$path=$dir.'/'.$new_nm;



$db->conn();

$db->query("INSERT INTO domain_tb
			(DOMAIN_NM, FILE_NM, FILE_PATH, CANDIDATE_CNT, CLUSTER_CNT, REGISTER_DT, REGISTER_USER)
			VALUES ('$domain', '$file_nm', '$path', 0, 0, now(), $user_cd);");
$domain_id = mysql_insert_id();
$FileClass->move_file($old_file,$path);


$file = fopen($path,"r");
$can_cnt=0;
$clu_cnt=0;
 while (($data = fgetcsv($file, 1000, ","))) {
 	$clu_cnt++;
    $num = count($data);
    $db->query("INSERT INTO cluster_tb
				(DOMAIN_CD, NER_CD, CLUSTER_ST, MERGING_ST)
				VALUES ($domain_id, $ner, 1, 0);");
	$cluster_id = mysql_insert_id();
    $db->query("INSERT INTO cluster_history_tb
				(CLUSTER_CD, OPERATION, USER_CD, REGISTER_DT)
				VALUES ($cluster_id, 1, $user_cd, now());");

	$db->query("INSERT INTO cluster_work_tb
				(CLUSTER_CD, STATUS_REGISTER_1, STATUS_DT_1)
				VALUES ($cluster_id, $user_cd, now());");

	$merging_cd=0;
	print_rr($data);
    for ($c=0; $c < $num; $c++) {
    	if(!empty($data[$c])){
    		$can_cnt++;
    		$fl=0;
    		if($c==0){$fl=1;}
			$can_nm  = iconv("euc-kr", "utf-8", $data[$c]);
			echoo($can_nm);
			$rt=$db->fetch("SELECT CLUSTER_CD FROM candidate_tb WHERE CANDIDATE_NM='$can_nm' LIMIT 1");

			if(!empty($rt)){
				$merging_cd=$rt[CLUSTER_CD];
			}

    		$db->query("INSERT INTO candidate_tb
				(CLUSTER_CD, CANDIDATE_NM, CANDIDATE_ST, REPRESENT_FL)
				VALUES ($cluster_id, '$can_nm', 1, $fl);");
			$candidate_id = mysql_insert_id();
			$db->query("INSERT INTO candidate_history_tb
					(CANDIDATE_CD, OPERATION, USER_CD, REGISTER_DT)
					VALUES ($candidate_id, 1, $user_cd, now());");

    	}
    }
	if(!empty($merging_cd)){
		$rt=$db->fetch("SELECT CLUSTER_ST FROM cluster_tb WHERE CLUSTER_CD=$merging_cd");
		if($rt[CLUSTER_ST]==9999){  //not use
			$db->query("UPDATE candidate_tb SET CLUSTER_CD =$merging_cd,REPRESENT_FL =0
				WHERE CLUSTER_CD = $cluster_id;");
			$db->query("DELETE FROM cluster_tb WHERE CLUSTER_CD = $cluster_id;");
			$db->query("INSERT INTO cluster_history_tb
				(CLUSTER_CD, OPERATION, USER_CD, REGISTER_DT)
				VALUES ($cluster_id, 9, $user_cd, now());");
			$db->query("INSERT INTO cluster_work_tb
						(CLUSTER_CD, STATUS_REGISTER_1, STATUS_DT_1,)
						VALUES ($cluster_id, $user_cd, now());");
		}else{
			$db->query("UPDATE cluster_tb SET MERGING_CLUSTER =$merging_cd, MERGING_ST = 1
				WHERE CLUSTER_CD in($cluster_id,$merging_cd);");
		}
	}

 }
$db->query("UPDATE domain_tb SET CANDIDATE_CNT = $can_cnt, CLUSTER_CNT = $clu_cnt
				WHERE DOMAIN_CD = $domain_id;");
fclose($file);


$db->close();

/*************************************************************************

	TEMPLATE PROCESS

***************************************************************************/
$JSClass->replace("./SynonymGenerator.php");



/*************************************************************************

	AJAX FUNCTION : Template_ function

***************************************************************************/



















?>
